﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using System.Net;
using System.IO;
using System.Diagnostics;

/// <summary>
/// Summary description for JsonParser
/// </summary>
public class JsonParser
{
    public static string serverPath = "http://fantasy.servebeer.com:8888";
    public static string userInformationPath = serverPath+"/userinformation?";
    public static string userPath = serverPath + "/user";
    public static string leaguePath = serverPath + "/league";
    public static string userTeamPath = serverPath + "/userteam";
    public static string userScorePath = serverPath + "/userscore";
    public static string teamPath = serverPath + "/team";
    public static string transferPath = serverPath + "/transfers";
    public static string watchlistPath = serverPath + "/watchlist";
    private JsonRequest jsonRequest;

    public User getUser(string username, string email)
    {
        string url = userInformationPath + "username=" + username+"&mail="+email;
        jsonRequest = new JsonRequest(url);
        string responseFromServer = jsonRequest.getRequest();

        User user = JsonConvert.DeserializeObject<User>(responseFromServer);
        return user;
    }

    public GameweekTeam getTeam(string gameweekNumber, string username)
    {
        string url = userTeamPath+"?gameweek=" + gameweekNumber + "&username=" + username;
        jsonRequest = new JsonRequest(url);
        string responseFromServer = jsonRequest.getRequest();
        GameweekTeam team = JsonConvert.DeserializeObject<GameweekTeam>(responseFromServer);
        team.gameweek = int.Parse(gameweekNumber);
        return team;
    }

    public UserTeam getUserTeam(string username)
    {
        string url = userInformationPath + "username=" + username;
        jsonRequest = new JsonRequest(url);
        string responseFromServer = jsonRequest.getRequest();
        UserTeam team = JsonConvert.DeserializeObject<UserTeam>(responseFromServer);
        return team;
    }


    public UserLeague getLeague(string username)
    {
        string url = leaguePath + "?username=" + username;
        jsonRequest = new JsonRequest(url);
        List<UserTeam> teams = null;
        string responseFromServer = jsonRequest.getRequest();
        teams = JsonConvert.DeserializeObject<List<UserTeam>>(responseFromServer);
        UserLeague league = new UserLeague();
        league.teams = teams;
        if (teams != null)
        {
            league.name = teams.ElementAt(0).leagueName;
        }
        return league;
    }

    public List<Player> getAllPlayers()
    {
        string url = teamPath;
        jsonRequest = new JsonRequest(url);
        List<Player> players = null;
        try
        {
            string responseFromServer = jsonRequest.getRequest();
            players = JsonConvert.DeserializeObject<List<Player>>(responseFromServer);
        }
        catch (Exception e)
        {
            Console.WriteLine("Players were not retrieved");
            Console.WriteLine(e.StackTrace);
        }
        return players;
    }

    public List<Player> getWatchList(string username, string password)
    {
        string url = watchlistPath;
        jsonRequest = new JsonRequest(url);
        List<Player> watchlist = null;
        try
        {
            string responseFromServer = jsonRequest.getRequestWithAuth(username, password);
            watchlist = JsonConvert.DeserializeObject<List<Player>>(responseFromServer);
        }
        catch (Exception e)
        {
            Console.WriteLine("Watchlist was not retrieved");
            Console.WriteLine(e.StackTrace);
        }
        return watchlist;
    }

    public Boolean addToWatchList(string username, string password, string playerID)
    {
        string url = watchlistPath;
        jsonRequest = new JsonRequest(url);
        Boolean success = jsonRequest.addToWatchlist(username, password, playerID);

        return success;
    }

    public Boolean removeFromWatchList(string username, string password, string playerID)
    {
        string url = watchlistPath;
        jsonRequest = new JsonRequest(url);
        Boolean success = jsonRequest.removeFromWatchlist(username, password, playerID);

        return success;
    }

    public List<Player> getAvailablePlayers(string username)
    {
        string url = transferPath;
        jsonRequest = new JsonRequest(url+"?username="+username);
        string responseFromServer = jsonRequest.getRequest();
        List<Player> players = JsonConvert.DeserializeObject<List<Player>>(responseFromServer);
        return players;
    }

    public Object getData(string url)
    {
        jsonRequest = new JsonRequest(url);
        string responseFromServer = jsonRequest.getRequest();

        Object obj = JsonConvert.DeserializeObject<Object>(responseFromServer);

        return obj;
    }

    public Boolean login(string username, string password)
    {
        string url = userPath;
        jsonRequest = new JsonRequest(url);
        Boolean success = jsonRequest.loginRequest(username, password);

        return success;
    }

    public Boolean registerUser(string userAsJson)
    {
        string url = userPath;
        jsonRequest = new JsonRequest(url);
        Boolean success = jsonRequest.postRequest(userAsJson, 202);
        return success;
    }

    public Boolean changePassword(string username, string currentPassword, string newPassword)
    {
        string url = userPath;
        jsonRequest = new JsonRequest(url);
        Boolean success = jsonRequest.changePasswordRequest(username, currentPassword, newPassword);

        return success;
    }

    public Boolean createUserTeam(string username, string password, string teamName)
    {
        string url = userTeamPath;
        jsonRequest = new JsonRequest(url);
        Boolean success = jsonRequest.createUserTeam(username, password, teamName);

        return success;
    }

    public Boolean createLeague(string username, string password, string leagueName)
    {
        string url = leaguePath;
        jsonRequest = new JsonRequest(url);
        Boolean success = jsonRequest.createUserLeague(username, password, leagueName);

        return success;
    }

    public Boolean addUserToLeague(string username, string password, string newPlayerUserName)
    {
        string url = leaguePath;
        jsonRequest = new JsonRequest(url);
        Boolean success = jsonRequest.addUserToLeague(username, password, newPlayerUserName);

        return success;
    }

    public Boolean pickPlayer(string username, string password, string playerID)
    {
        string url = userTeamPath;
        jsonRequest = new JsonRequest(url);
        Boolean success = jsonRequest.pickPlayer(username, password, playerID);

        return success;
    }

    public Boolean subPlayer(string username, string password, string playerIn, string playerOut)
    {
        string url = transferPath;
        jsonRequest = new JsonRequest(url);
        Boolean success = jsonRequest.subPlayer(username, password, playerIn, playerOut);

        return success;
    }

    public Boolean transferPlayer(string username, string password, string playerIn, string playerOut)
    {
        string url = transferPath;
        jsonRequest = new JsonRequest(url);
        Boolean success = jsonRequest.transferPlayer(username, password, playerIn, playerOut);

        return success;
    }

    public List<DraftPick> getDraft(string username)
    {
        List<DraftPick> draft = new List<DraftPick>();
        string url = serverPath + "/draft?username=" + username;
        jsonRequest = new JsonRequest(url);
        string responseFromServer = jsonRequest.getRequest();
        draft = JsonConvert.DeserializeObject<List<DraftPick>>(responseFromServer);
        return draft;
    }

    public Boolean draftPlayer(string username, string password, string playerID)
    {
        string url = serverPath + "/drafttransfer";
        jsonRequest = new JsonRequest(url);
        Boolean success = jsonRequest.pickPlayer(username, password, playerID);

        return success;
    }
}